<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 1.生成card -->
      <el-card class="box-card">
        <!-- 2.生成tabs标签栏 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 3.引入layout布局,一半一半 这里是头部-->
            <el-row class="department-header">
              <!-- 左侧写公司名称 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧写操作/负责人 -->
              <el-col :span="4">
                <!-- 复用el-row,右侧操作 -->
                <el-row type="flex">
                  <el-col>负责人</el-col>
                  <el-col>
                    <!-- 操作运用下拉菜单 -->
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        下拉菜单<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="deptFn('')">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDept('')">编辑部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 树形结构展示数据 -->
            <el-tree
              class="departments-tree"
              :data="treeData"
              default-expand-all
              :props="defaultProps"
            >
              <template v-slot="{data}">
                <el-row justify="space-between" style="width: 100%">
                  <!-- 左侧写公司名称 -->
                  <el-col :span="20">
                    <span> {{ data.name }}</span>
                  </el-col>
                  <!-- 右侧写操作/负责人 -->
                  <el-col :span="4">
                    <!-- 复用el-row,右侧操作 -->
                    <el-row type="flex">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <!-- 操作运用下拉菜单 -->
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            下拉菜单<i
                              class="el-icon-arrow-down el-icon--right"
                            />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="deptFn(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title="isEdit?'编辑组织架构':'新增组织架构'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- v-if,关闭弹窗销毁组件 -->
          <dept-dialog
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @upload-list="uploadList"
            @close="closeConfirm"
          />
        </el-dialog>

      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arrToTree } from '@/utils'
import DeptDialog from './DeptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      defaultProps: {
        children: 'children', // 展示的子组件名字
        label: 'name' // 展示给用户看的文字
      },
      showDialog: false,
      parentId: '',
      isEdit: false, // 编辑状态
      originList: []// 验证code,向子组件传递的精简数组
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获得扁平数据,渲染列表
    async getDepartments() {
      const res = await getDepartments()
      this.treeData = arrToTree(res.data.depts, '')
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        code: item.code,
        id: item.id,
        pid: item.pid
      }))
    },

    // 添加子部门,弹框显示
    deptFn(id) {
      this.showDialog = true
      this.isEdit = false
      this.parentId = id
    },

    // 子传父更新列表
    uploadList() {
      this.showDialog = false
      this.getDepartments()
    },

    // 关闭弹窗
    closeConfirm() {
      this.showDialog = false
    },

    // 编辑功能
    editDept(id) {
      this.showDialog = true
      this.isEdit = true
      this.parentId = id
    },
    // 删除部门
    async delDept(id) {
      // 1.点击删除出现提示框
      const confirmRes = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击取消
      if (confirmRes === 'cancel') return this.$message('已取消')
      // 点击确定 1.发送请求,后台删除数据
      const res = await delDepartment(id)
      // 2.给用户提示
      this.$message.success(res.message)
      // 3.渲染列表
      this.getDepartments()
    }

  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }
  .departments-tree {
    margin: 12px 0px;
  }
  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
